package com.duanjc.shiro.config;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.duanjc.shiro.entity.TPerms;
import com.duanjc.shiro.entity.TUser;
import com.duanjc.shiro.service.TUserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * [一句话描述该类的功能]
 *
 * @author : [duanjiangcheng]
 * @version : [v1.0]
 * @createTime : [2022/11/29 16:08]
 */
@Slf4j
@Component
public class CustomerRealm extends AuthorizingRealm {

    @Autowired
    private TUserService tUserService;
    
    /**
     * description: 自定义授权方法 <br>
     * version: 1.0 <br>
     * date: 2022/11/29 16:10 <br>
     * author: duanjiangcheng <br>
     * @param principalCollection
     * @return org.apache.shiro.authz.AuthorizationInfo
     */ 
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        log.info("开始权限验证");
        //获取主身份信息
        String primaryPrincipal = (String) principalCollection.getPrimaryPrincipal();
        //根据主身份信息获取角色 和 权限信息
        TUser user = tUserService.findRolesByUserName(primaryPrincipal);
        //授权角色信息
        if (!CollectionUtils.isEmpty(user.getRoles())){

            SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();
            user.getRoles().forEach(role -> {

                simpleAuthorizationInfo.addRole(role.getName());
                //权限信息
                List<TPerms> perms = tUserService.findPermsByRoleId(role.getId());
                if (!CollectionUtils.isEmpty(perms)){

                    perms.forEach(perm -> {

                        simpleAuthorizationInfo.addStringPermission(perm.getName());
                    });
                }
            });
            return simpleAuthorizationInfo;
        }
        return null;
    }

    /**
     * description: 自定义认证 <br>
     * version: 1.0 <br>
     * date: 2022/11/29 16:10 <br>
     * author: duanjiangcheng <br>
     * @param authenticationToken
     * @return org.apache.shiro.authc.AuthenticationInfo
     */ 
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {

        String principal = (String) authenticationToken.getPrincipal();
        TUser user = tUserService.getOne(new QueryWrapper<TUser>().eq("username",principal));
        if (!ObjectUtils.isEmpty(user)){
            return new SimpleAuthenticationInfo(user.getUsername(),user.getPassword(), new MyByteSource(user.getSalt()),this.getName());
        }
        return null;
    }
}
